package com.community.contents.controller;



import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.core.constant.RedisKey;
import com.community.common.core.utils.Result;
import com.community.common.security.dto.UserLoginDto;
import com.community.contents.dto.PostDTO;
import com.community.contents.entity.DiscussEntity;
import com.community.contents.entity.PostEntity;
import com.community.contents.entity.UserEntity;
import com.community.contents.service.IPostService;
import com.community.contents.service.IUserService;
import com.community.contents.vo.PostVo;
import com.community.contents.vo.TopicVo;
import io.swagger.annotations.Api;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.Date;


/**
 * 帖子
 *
 * @author hjs hjs@qq.com
 * @since 1.0.0 2023-02-21
 */
@RestController
@RequestMapping("contents/post")
@Api(tags = "帖子")
public class PostController {
    @Autowired
    private IPostService postService;


    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("page")
    public Result<Page<PostDTO>> page(@RequestBody Page<PostDTO> params) {
        postService.pageDto(params);
        return new Result<Page<PostDTO>>().ok(params);
    }


    @PostMapping
    public boolean save(@RequestBody PostEntity dto) {
        return postService.save(dto);
    }

    //发布动态--帖子
    @PostMapping("release")
    public Result release(@RequestBody PostEntity postEntity, UserLoginDto userLoginDto) {
        String discussList = postEntity.getDiscussList();
        //设置发帖用户ID，发帖时间
        postEntity.setUserId(userLoginDto.getUserId());
        postEntity.setCreateTime(new Date());
        boolean save = postService.save(postEntity);
        if (save) {
            return new Result().ViewOk(postEntity.getId());
        }
        return new Result().error("发布失败");
    }

    /*
     * detail  跳转到帖子详情页--view
     * @Description:获取帖子详情
     * @Date: 2023/3/3 22:58
     * @Author: hjs
     * @param id: 帖子id
     * @return: com.community.common.core.utils.Result
     **/
    @GetMapping("detail")
    public Result detail(Integer id) {
        PostVo postVo = postService.detail(id);
        return new Result().ViewOk(postVo);
    }


    /**
     * <p>
     * 查询我的帖子的方法
     * </p>
     */

    @GetMapping("releaseList")
    public Result releaseList(@RequestParam("page") Integer current, UserLoginDto userLoginDto) {

        //1.获取前端传过来的用户数据

        Page<PostVo> p = new Page<>();
        p.setCurrent(current);
        postService.releaseList(p, userLoginDto.getUserId());
//        postService.releaseList(p,5);

        return new Result().ViewOk(p);
    }

    @GetMapping("hotHuaTi")
    public Result hotHuaTi() {
        Set range = redisTemplate.opsForZSet().reverseRange(RedisKey.HOT_HUATI, 0, 2);
        return new Result().ViewOk(JSON.toJSONString(range));
    }

    /**
     *查询圈子下的帖子列表PostVo
     **/
    @GetMapping("list")
    public Result list( Page<PostVo> page, Integer topicId) {
        postService.postList(page, topicId);
        return new Result().ViewOk(page);
    }


    /**
     * userList
     *
     * @Description: 查询圈子下发过帖子的用户列表
     * @Date: 2023/3/7 17:02
     * @Author: hjs
     * @return: com.community.common.core.utils.Result
     **/
    @GetMapping("userList")
    public Result userList(@RequestParam("id") Integer topicId, Page page) {
        Page<UserEntity> list = postService.userList(topicId, page);
        return new Result().ViewOk(list);
    }
}